package ycj.learn.jvm;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class VolatileTest {

    private static volatile int race = 0;
    private static AtomicInteger arace = new AtomicInteger(0);

    public static void increase() {
        race++;
    }
    public static void aIncrease() {
        arace.incrementAndGet();
    }

    private static final int THREAD_COUNT = 20;

    public static void main(String[] args) throws InterruptedException {

        Thread[] threads = new Thread[THREAD_COUNT];

        ExecutorService threadPool = Executors.newFixedThreadPool(20);

        for (int i = 0; i < THREAD_COUNT; i++) {
            threadPool.execute(() -> {
                for (int j = 0; j < 10000; j++) {
                    increase();
                    aIncrease();
                }
            });
        }

        Thread.sleep(1000);
        System.out.println(race);
        System.out.println(arace.get());
        Thread.sleep(1000);
        System.out.println(race);
        System.out.println(arace.get());
        threadPool.shutdown();
    }

}
